#include "value.h"
#include "operator.h"
#include "expressionwriter.h"

#include <QDomElement>

#include <QLocale>

Cn::Cn(Object const *o)
	: Object(Object::value), m_value(0.), m_format(Real)
{
    if(o->type()==Operator::value)
    {
        Cn *v = (Cn*) o;
        m_value = v->value();
        m_format = v->format();
        setCorrect(v->isCorrect());
    } else
    {
        setCorrect(false);
    }
}

QString Cn::visit(ExpressionWriter* e) const
{
	return e->accept(this);
}

void Cn::setValue(const QDomElement& val)
{
// 	this->m_vformat=whatValueFormat(val);
    bool wrong;
    QString tag = val.tagName();
    m_format=Real;

    if(tag == "cn"){ // a is a number
        if(val.attribute("type", "integer") == "real")
        {
            m_value= val.text().trimmed().toDouble(&wrong); //TODO: Base on double not implemented
        } else if(val.attribute("type", "integer") == "integer")
        {
            int base = val.attribute("base", "10").toInt(NULL, 10);
            m_value= val.text().trimmed().toInt(&wrong, base);
            m_format=Integer;
        }
        else if(val.attribute("type") == "constant")
        {
            if(val.text() == "&pi;")
                m_value = pi().m_value;
            else if (val.text() == "&ee;" || val.text() == "&ExponentialE;")
                m_value = e().m_value;
            else if (val.text() == "&true;")	{m_value=1.; m_format=Boolean;}
            else if (val.text() == "&false;")	{ m_value=0.; m_format=Boolean; }
            else if (val.text() == "&gamma;")	{ m_value = 0.5772156649; }
        }
    }
}

Cn Cn::pi() { return Cn(3.1415926535897932384626433); }
Cn Cn::e() { return Cn(2.718281828); }
Cn Cn::euler() { return Cn(0.5772156649); }

